package com.ego.passport.controller;

import com.ego.commons.constant.Constant;
import com.ego.commons.pojo.EgoResult;
import com.ego.passport.service.PassportService;
import com.ego.pojo.TbUser;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.regex.Pattern;

@Controller
public class PassportController {


    @Autowired
    private PassportService passportService;


    @RequestMapping("/user/showLogin")
    public String userShowLogin(@RequestHeader(name = "Referer") String referer, Model model, HttpServletRequest  httpRequest){

       // model.addAttribute("redirect",referer);


        return "login";
    }

    @RequestMapping("/user/showRegister")
    public String userSHowRegister(){

        return "register";
    }


    @GetMapping("/user/check/{param}/{type}")
    @ResponseBody
    public EgoResult userCheck(@PathVariable String param,@PathVariable int type){

        EgoResult result = passportService.checkUser(param,type);

        return result;
    }


    @RequestMapping("/user/register")
    @ResponseBody
    public EgoResult userRegister(TbUser tbUser){
        System.out.println(tbUser);

        String username = tbUser.getUsername();
        String phone = tbUser.getPhone();
        String email = tbUser.getEmail();


        if(StringUtils.isBlank(username) || StringUtils.isNotBlank(username) && !username.matches("^[a-zA-Z]\\w{5,15}$")){
            return EgoResult.error("账号必须首字母开头，长度6-16");
        }else if(StringUtils.isBlank(email) || StringUtils.isNotBlank(email) && !email.matches("^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$")){
            return EgoResult.error("请输入正确邮箱");
        }else if(StringUtils.isBlank(phone) || StringUtils.isNotBlank(phone) && !phone.matches("^1[3|4|5|6|7|8|9]\\d{9}$")){
            return EgoResult.error("请输入正确的手机号");
        }


        return passportService.register(tbUser);
    }


    @RequestMapping("/user/login")
    @ResponseBody
    public EgoResult userLogin(TbUser tbUser, HttpSession httpSession){

        System.out.println("httpSession.getId() = " + httpSession.getId());

        TbUser loginUser  = passportService.login(tbUser);

        if(loginUser!=null){
            //将登陆的用户共享到Session中
            httpSession.setAttribute(Constant.LOGIN_USER_IN_SESSION,loginUser);
            return EgoResult.ok();
        }
        return EgoResult.error("账号密码错误，请重新登录");

    }


    @RequestMapping("/user/token/{token}")
    @CrossOrigin(allowCredentials="true") //允许接收跨域请求的Cookie，默认是不允许接收跨域请求的Cookie
    @ResponseBody
    public EgoResult userToken(HttpSession httpSession){

        TbUser loginUser = (TbUser) httpSession.getAttribute(Constant.LOGIN_USER_IN_SESSION);

        if(loginUser!=null){
            return EgoResult.ok(loginUser);
        }
        return EgoResult.error("没有登录..");

    }


    @RequestMapping("/user/logout/{token}")
    @ResponseBody
    @CrossOrigin(allowCredentials="true")
    public EgoResult userLogout(HttpSession httpSession){

        httpSession.removeAttribute(Constant.LOGIN_USER_IN_SESSION);
        //清除Session中数据
        httpSession.invalidate();


        return EgoResult.ok("退出登录成功");
    }

}
